package com.bdqn.t382.lx.controller;

import com.alibaba.fastjson2.JSON;
import com.bdqn.t382.lx.pojo.BookManage;
import com.bdqn.t382.lx.pojo.Page;
import com.bdqn.t382.lx.pojo.SysUser;
import com.bdqn.t382.lx.pojo.vo.R;
import com.bdqn.t382.lx.service.BookManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookManageController {
    private Logger logger = LoggerFactory.getLogger(BookManageController.class);
    @Autowired
    BookManageService bookManageService;

    @ResponseBody
    @RequestMapping("/list")
    public List list(Model model) {
        List<BookManage> list = bookManageService.getBookManageList();
        model.addAttribute("list", list);
        return list;
    }

    @RequestMapping("/add")
    public String add(Model model) {
        return "add";
    }

    @ResponseBody
    @RequestMapping("/formAdd")
    public int formAdd(Model model, @RequestBody BookManage bookManage) {
        int count = bookManageService.insertBookManage(bookManage);
        return count;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public int delete(Model model, @RequestParam int b_id) {
        int count = bookManageService.deleteBookManage(b_id);
        return count;
    }

    @ResponseBody
    @RequestMapping("/view")
    public BookManage view(Model model, @RequestParam int b_id) {
        // 根据 b_id 查询图书信息
        BookManage book = bookManageService.viewBookManage(b_id);
        model.addAttribute("book", book);
        return book;
    }

    @ResponseBody
    @RequestMapping("/formUpdate")
    public int formView(Model model, @RequestBody BookManage bookManage) {
        int count = bookManageService.updateBookManage(bookManage);
        return count;
    }

    @ResponseBody
    @RequestMapping("/page")
    public Page page(Model model,
                     @RequestParam Integer pageNo,
                     @RequestParam Integer pageSize, @RequestParam String b_name) {
        Page page = bookManageService.getPage(pageNo, pageSize, b_name);
        model.addAttribute("page", page);
        return page;
    }

    @RequestMapping("/login")
    public String login(Model model) {
        return "login";
    }

    @ResponseBody
    @RequestMapping("/doLogin")
    public R doLogin(@RequestBody SysUser sysUser) {
        SysUser user = null;
        if (sysUser.getAccount().length() < 1 || sysUser.getAccount() == null) {
            return R.fail();
        } else {
            user = bookManageService.getLogin(sysUser.getAccount(), sysUser.getPassword());
            if (user == null) {
                logger.info(sysUser.getAccount() + "用户不存在");
                return R.fail();
            } else if (!user.getPassword().equals(sysUser.getPassword())) {
                logger.info(sysUser.getAccount() + "密码错误");
                return R.fail();
            } else {
                logger.info(sysUser.getAccount() + "登录成功");
                return R.ok(user);
            }
        }
    }

    @ResponseBody
    @RequestMapping("/getSysuserById")
    public SysUser getSysuserById(@RequestParam int id) {
        SysUser user = bookManageService.getSysuserById(id);
        return user;
    }

    @RequestMapping("/save")
    public String save(Model model) {
        return "save";
    }

    @ResponseBody
    @RequestMapping("/accountExist")
    public String accountExist(SysUser sysUser, Model model) {
        HashMap<String, Object> resultMap = new HashMap();
        SysUser login = bookManageService.getLogin(sysUser.getAccount(), sysUser.getPassword());
        if (login != null) {
            resultMap.put("exist", 1);
        } else {
            resultMap.put("exist", 0);
        }
        return JSON.toJSONString(resultMap);
    }
}
